/**
 * @description webpack common config
 */

const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const { srcPath, distPath } = require('./constants')

module.exports = {
  entry: path.join(srcPath, 'index'),
  output: {
    path: distPath
  },
  module: {
    rules: [
      // babel
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: srcPath,
        exclude: /node_modules/
      },
      // webpack 默认只处理 js 文件，这里要单独对 css 文件进行处理
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      }
    ]
  },
  plugins: [
    // 每次打包清理之前的旧文件
    new CleanWebpackPlugin(),
    new CopyPlugin({
      patterns: [
        // 拷贝 favicon.ico
        {
          from: path.join(srcPath, 'favicon.ico'),
          to: path.join(distPath, 'favicon.ico')
        }
        // 拷贝 MP_verify_CF2giMLYs5C9eiKV.txt ，用于微信分享
        // {
        //   from: path.join(srcPath, 'MP_verify_CF2giMLYs5C9eiKV.txt'),
        //   to: path.join(distPath, 'MP_verify_CF2giMLYs5C9eiKV.txt')
        // }
      ]
    })
  ]
}
